package com.beaven.imclient.presenter.account;

import android.text.TextUtils;
import com.beaven.imclient.R;
import com.beaven.model.Factory;
import com.beaven.model.data.DataSource;
import com.beaven.model.data.helper.AccountHelper;
import com.beaven.model.data.helper.DbHelper;
import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.api.account.AccountRspModel;
import com.beaven.model.model.api.account.RegisterModel;
import com.beaven.model.model.db.User;
import com.beaven.model.model.sp.Account;
import java.util.regex.Pattern;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;

/**
 * author: Beaven
 * time: 2017/5/22 16:36
 */

public class RegisterPresenter implements RegisterContract.Presenter, DataSource.FailedCallBack {

  private RegisterContract.View view;

  private CompositeSubscription subscription;

  public RegisterPresenter(RegisterContract.View view) {
    this.view = view;
    subscription = new CompositeSubscription();
  }

  @Override public void subscribe() {

  }

  @Override public void unSubscribe() {
    view.hideLoading();
    subscription.clear();
  }

  @Override public void register() {
    String account = view.getAccount();
    String name = view.getNickName();
    String password = view.getPassword();
    if (!checkMobile(account)) {
      // 提示
      view.showText(R.string.data_account_register_invalid_parameter_mobile);
    } else if (name.length() < 2) {
      // 姓名需要大于2位
      view.showText(R.string.data_account_register_invalid_parameter_name);
    } else if (password.length() < 6) {
      // 密码需要大于6位
      view.showText(R.string.data_account_register_invalid_parameter_password);
    } else {
      // 进行网络请求
      // 构造Model，进行请求调用
      view.showLoading();
      RegisterModel model = new RegisterModel(account, password, name, Account.getPushId());
      // 进行网络请求，并设置回送接口为自己
      subscription.add(
          AccountHelper.register(model).subscribe(new Subscriber<RspModel<AccountRspModel>>() {
            @Override public void onCompleted() {

            }

            @Override public void onError(Throwable e) {
              view.showText(R.string.data_network_error);
              view.hideLoading();
            }

            @Override public void onNext(RspModel<AccountRspModel> accountRspModelRspModel) {
              if (accountRspModelRspModel.isSuccess()) {
                User user = accountRspModelRspModel.getResult().getUser();
                DbHelper.instance().saveModel(User.class, user);
                // 登录信息保存
                Account.login(accountRspModelRspModel.getResult());
                Account.setBind(true);
                view.startMain();
              } else {
                Factory.decodeRspCode(accountRspModelRspModel, RegisterPresenter.this);
              }
              view.hideLoading();
            }
          }));
    }
  }

  private boolean checkMobile(String phone) {
    return !TextUtils.isEmpty(phone) && Pattern.matches(LoginPresenter.REGEX_MOBILE, phone);
  }

  @Override public void loadDataFail(int strRes) {
    view.showText(strRes);
  }
}
